package com.another.bidservice.service.impl;

import com.another.bidservice.entity.TPort;
import com.another.bidservice.dao.jpa.PortJPADao;
import com.another.bidservice.dao.mapper.PortDao;
import com.another.bidservice.service.IPortService;
import com.another.common.enums.CodeRuleEnum;
import com.another.common.utils.common.DateUtil;
import com.another.common.utils.common.EmptyUtil;
import com.another.common.utils.common.MyException;
import com.another.common.utils.result.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PortServiceImpl extends BaseServiceImpl implements IPortService {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private PortDao portDao;

    @Autowired
    private PortJPADao portJPADao;

    @Override
    public Result getList(TPort tPort) {
        String sql = "select * from db_sys.vt_port";
        List<TPort> list = portDao.getList(sql);
        if (EmptyUtil.isEmpty(list)) {
            return Result.success("没有查询到数据！", "");
        } else {
            return Result.success(list);
        }
    }


    @Override
    public Result insertList(List<TPort> tPorts) {
        int ret = portDao.insertList(tPorts);
        if (ret == tPorts.size()) {
            return Result.success("添加成功！", "");
        } else {
            throw new MyException(101, "添加失败！");
        }
    }


    @Override
    public Result deleteList(List<Integer> rowid) {
        int ret = portDao.deleteList(rowid);
        if (ret == rowid.size()) {
            return Result.success("删除成功！", "");
        } else {
            throw new MyException(101, "删除失败！");
        }
    }

    @Override
    public Result updateList(List<TPort> tPorts) {
        try {
            int ret = portDao.updateList(tPorts);
            return Result.success("修改成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "修改失败！");
        }
    }


    @Override
    public Result insertPort(TPort tPort) {
        try {

            if (EmptyUtil.isEmpty(tPort.getPortType())) {
                return Result.error("未设置港口类型！");
            }
            if (EmptyUtil.isEmpty(tPort.getPortName())) {
                return Result.error("未设置港口！");
            }
            if (EmptyUtil.isEmpty(tPort.getDockName())) {
                return Result.error("未设置码头！");
            }

            //查看港口是否存在
            List<TPort> list = portJPADao.findAllByPortNameAndDockName(tPort.getPortName(), tPort.getDockName());
            if (EmptyUtil.isNotEmpty(list)) {
                return Result.error("港口码头已存在!");
            }


            String portNo = generateNumber(CodeRuleEnum.PORT_NO.getCode());

            tPort.setPortNo(portNo);
            tPort.setCreateId(tPort.getRequestUserId());
            tPort.setCreateDate(DateUtil.getNowDateByyyyyMMddHHmmss());

            tPort = portJPADao.save(tPort);
            return Result.success("添加成功！", tPort);
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "添加失败！");
        }
    }

    @Override
    public Result updatePort(TPort tPort) {
        try {

            if (EmptyUtil.isEmpty(tPort.getPortNo())) {
                return Result.error("港口编号参数未传！");
            }

            tPort.setUpdateId(tPort.getRequestUserId());

            int ret = portDao.updatePort(tPort);
            return Result.success("修改成功！");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "修改失败！");
        }
    }

    @Override
    public Result deletePort(TPort tPort) {
        try {
            int ret = portDao.deletePort(tPort);
            return Result.success("删除成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "删除失败！");
        }
    }
}



